package org.hot100_6.leetcode49;

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        String [] strs = {"eat","tea","tan","ate","nat","bat"};
        for (List<String> groupAnagram : groupAnagrams(strs)) {
            for (String s : groupAnagram) {
                System.out.println(s);
            }

            System.out.println("---------------");

        }

    }

    public static List<List<String>> groupAnagrams(String[] strs) {

        HashMap<String, List<String>> res = new HashMap<>();

        List<List<String>> resList = new ArrayList<>();
        for (String str : strs) {
            char[] strChars = str.toCharArray();

            Arrays.sort(strChars);

            /**
             *
             这段代码生成的字符串是类名+hash码
             String sortStr = strChars.toString();
             */
            String sortStr = new String(strChars);

            if (res.containsKey(sortStr)) {
                List<String> strList = res.get(sortStr);
                strList.add(str);
            } else {
                ArrayList<String> strList = new ArrayList<>();
                strList.add(str);
                res.put(sortStr, strList);
            }
        }

        for (Map.Entry<String, List<String>> item : res.entrySet()) {
            resList.add(item.getValue());
        }


        return resList;
    }
}
